//
//  ResetPassView1.m
//  specialine
//
//  Created by 往返网络 on 17/2/10.
//  Copyright © 2017年 com. All rights reserved.
//

#import "ResetPassView1.h"

#define ReturnTag 1000

#define SendYanzhengTag 2000

#define ResetEnterType @"loginaccess"  //进入重设密码页面（1）／手机验证页面（2）

@interface ResetPassView1 ()<UITextFieldDelegate>


@end


@implementation ResetPassView1
{
    
    UITextField * phonetf;
    
    UIButton * yanzhengbtn;
    
    
}
- (instancetype)initWithFrame:(CGRect)frame andTitle:(NSString *)title{
    
    NSLog(@"title is %@",title);

    if (self = [super initWithFrame:frame]) {
        
        [self createUIWithTitle:title];
        
        
    }

    return self;
    
}
- (void)createUIWithTitle:(NSString *)title{
    
    //适配字体大小,圆角大小
    
    float fontsie = 15;
    
    float cornersize = 20;
    
    fontsie = SCREEN_H < 667 ? 13 :  17;
    
    cornersize = SCREEN_H < 667 ? 18 : 22 ;
    
    
    if (SCREEN_H == 667) {
        
        fontsie = 15;
        
        cornersize = 20;
        
    }
    
    self.backgroundColor = [UIColor whiteColor];
    
    UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20, 80.0/W_BASE*SCREEN_W, 44)];
    
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    UIFont * iconfont = [UIFont fontWithName:@"iconfont" size:fontsie+2];
    
    btn.titleLabel.font = iconfont;
    
    [btn setTitle:@"\U0000e69a" forState:UIControlStateNormal];
    
    [self addSubview:btn];
    
    btn.tag = ReturnTag;
    
    [btn addTarget:self action:@selector(enterClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel * titlelab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, SCREEN_W, 44)];
    
    titlelab.textAlignment = NSTextAlignmentCenter;
    
    titlelab.font = [UIFont systemFontOfSize:fontsie+2];
    
    titlelab.textColor = [UIColor blackColor];
    
    titlelab.text = title;

    
    [self addSubview:titlelab];
    
    phonetf = [[UITextField alloc]initWithFrame:CGRectMake(65.0/W_BASE*SCREEN_W, 64+20.0/H_BASE*SCREEN_H, 605.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    phonetf.layer.borderWidth = 1;
    
    phonetf.font = [UIFont systemFontOfSize:fontsie];
    
    phonetf.keyboardType  = UIKeyboardTypeNumberPad;
    //设置tf的缩紧，靠右缩紧，给tf加一个leftview
    
    UIView * leftview  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    phonetf.leftView = leftview;
    
    phonetf.leftViewMode = UITextFieldViewModeAlways;
    
    phonetf.layer.borderColor = [[UIColor colorWithRed:239.0/255 green:239.0/255 blue:239.0/255 alpha:1]CGColor];
    
    phonetf.placeholder = @"手机号";
    
    phonetf.clearButtonMode = UITextFieldViewModeAlways;
    
    phonetf.layer.cornerRadius = cornersize;
    
    [self addSubview:phonetf];
    
    phonetf.delegate = self;
    
    
    yanzhengbtn = [[UIButton alloc]initWithFrame:CGRectMake(65.0/W_BASE*SCREEN_W, CGRectGetMaxY(phonetf.frame)+20.0/H_BASE*SCREEN_H, 605.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    
    yanzhengbtn.backgroundColor = [UIColor colorWithRed:47.0/255 green:148.0/255 blue:251.0/255 alpha:0.5];
    
    [yanzhengbtn setTitle:@"发送验证码" forState:UIControlStateNormal];
    
    yanzhengbtn.layer.cornerRadius = cornersize;
    
    yanzhengbtn.titleLabel.font = [UIFont systemFontOfSize:fontsie];
    
    [self addSubview:yanzhengbtn];
    
    yanzhengbtn.tag = SendYanzhengTag;
    
    [yanzhengbtn addTarget:self action:@selector(enterClick:) forControlEvents:UIControlEventTouchUpInside];
    
    yanzhengbtn.userInteractionEnabled = NO;
    
    
}
#pragma mark --- Action- -- - - --
- (void)enterClick:(UIButton *)btn{


    NSInteger btntag = btn.tag;
    
    NSString * phonestr = phonetf.text;
    
    switch (btntag) {
        case  ReturnTag:
            
            //调用代理方法，返回登陆界面
            
            [self.resetpassdelegate1 enterResetPassVC2BtnType:ResetPassView1BtnClickReturn andPhoneStr:nil];
            
            break;
            
            case SendYanzhengTag:
            
            // 调用代理方法，(检查手机号规格)发送验证码，进入下一页,传值
            
            [self.resetpassdelegate1 enterResetPassVC2BtnType:ResetPassView1BtnClickSendYanzhengCode andPhoneStr:phonestr];
            
            
        default:
            
            break;
    }
    

}
#pragma mark ----- ----------textfiled Delegate---------------------


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    
    
    
    //一旦有输入1个字符，就变色
    
    NSString * textstring = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    
    if (textstring.length >= 1) {
        
        yanzhengbtn.backgroundColor = [UIColor colorWithRed:47.0/255 green:148.0/255 blue:251.0/255 alpha:1];
        
        yanzhengbtn.userInteractionEnabled = YES;
        
        
    }

    //检测输入位数，输入到第11位时，下方按钮透明度为1
    
    //获取 当前tf的string ,并测出长度
    
    NSString * text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
//    if (text.length >= 11) {
//        
//        
//        yanzhengbtn.backgroundColor = [UIColor colorWithRed:47.0/255 green:148.0/255 blue:251.0/255 alpha:1];
//        
//        yanzhengbtn.userInteractionEnabled = YES;
//        
//    }
    
    
    return YES;
    
    
    
    
    
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [self endEditing:YES];
    

    return YES;

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [phonetf resignFirstResponder];
    
    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
